package com.pansoft.openplanet.util;

import android.content.Context;
import android.util.Log;

import com.pansoft.openplanet.bean.Account;
import com.pansoft.openplanet.db.AccountDao;

import org.bitcoinj.core.ECKey;
import org.bitcoinj.crypto.DeterministicKey;
import org.bitcoinj.wallet.Wallet;
import org.web3j.crypto.CipherException;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.ECKeyPair;
import org.web3j.crypto.WalletUtils;

import java.io.File;
import java.io.IOException;
import java.math.BigInteger;

/**
 * Created by YQS on 2018/3/28.
 */

public class PrivateKeyUtils {

    private static final String TAG = "PrivateKeyUtils";

    /**
     * 导出私钥(请勿在主线程执行)
     *
     * @param keystorePath 账号的keystore路径
     * @param password     密码
     */
    public static String exportPrivateKey(String keystorePath, String password) {
        try {
            Credentials credentials = WalletUtils.loadCredentials(
                    password,
                    keystorePath);
            BigInteger privateKey = credentials.getEcKeyPair().getPrivateKey();
            String key = TCAccountManager.convertTo16Radix(privateKey.toString());
//            String key = privateKey.toString(16);
            Log.i(TAG, "exportPrivateKey: " + key);
            return key;
        } catch (Exception e) {
            e.printStackTrace();
            return null;

        }
    }
    /**
     * 导出BTC私钥(请勿在主线程执行)
     *
     * @param keystorePath 账号的keystore路径
     * @param password     密码
     */
    public static String exportBtcPrivateKey(String keystorePath, String password) {
        try {
            Wallet wallet = Wallet.loadFromFile(new File(keystorePath));
           System.out.println(wallet.toString());
            DeterministicKey deterministicKey = wallet.currentReceiveKey();
            BigInteger privKeyBTC = deterministicKey.getPrivKey();
            //生成eckey
            ECKey ecKey = ECKey.fromPrivate(privKeyBTC);
            //私钥
            String privateKey = ecKey.getPrivateKeyAsWiF(BtcNetParamUtil.getBtcNetParams()).toString();
            return privateKey;
        } catch (Exception e) {
            e.printStackTrace();
            return null;

        }
    }
    /**
     * 导出BTC私钥(请勿在主线程执行)
     *
     * @param address 账号地址
     * @param password 密码
     */
    public static String exportBtcMnemonic(Context context, String address, String password) {
        try {
            Account account = new AccountDao(context).getAccount(address);
            String encryptedMnemonic = account.getSecretMnemonic();
            String mnemonic = TCAccountManager.decryptByMainPrivKey(encryptedMnemonic);
            return mnemonic;
        } catch (Exception e) {
            e.printStackTrace();
            return null;

        }
    }
    /**
     * 导出EOS私钥(请勿在主线程执行)
     *
     * @param secretPrivateKey 加密的私钥
     * @param password     密码
     */
    public static String exportEosPrivateKey(String secretPrivateKey, String password) {
        String privateKey = null;
        try {
            privateKey = TCAccountManager.decryptByMainPrivKey(secretPrivateKey);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return privateKey;
    }
    /**
     * 导入私钥
     *
     * @param privateKey 私钥
     * @param password   密码
     * @param directory  存储路径 默认测试网络WalletUtils.getTestnetKeyDirectory() 默认主网络 WalletUtils.getMainnetKeyDirectory()
     */
    private static void importPrivateKey(BigInteger privateKey, String password, String directory) {
        ECKeyPair ecKeyPair = ECKeyPair.create(privateKey);
        try {
            String keystoreName = WalletUtils.generateWalletFile(password,
                    ecKeyPair,
                    new File(directory),
                    true);
            System.out.println("keystore name " + keystoreName);
        } catch (CipherException | IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        exportBtcPrivateKey("C:\\Users\\YQS\\Documents\\Tencent Files\\978298753\\FileRecv\\MobileFile\\BTC--2018-06-27T10-58-50.096--1LQJGL9Vx5GeCz1oQn.json","");
    }
}
